<?php
namespace Admin\Controller;
use Home\Controller\Home2017Controller;

class ArticleController extends CheckController{
    //分配Article模板页面
    function index(){
        $this->pa=M('setting_page1')->where("id='1'")->getField('page');
        $id=isset($_GET['id'])?I('get.id'):'';
        $search=isset($_GET['search'])?I('get.search'):'';
        $search = gpc_get(safe_replace(urldecode($search)));  //搜索关键字
        if($id!=0){
            $where="and search=1";
        }else{
            $where="search=1";
        };
        if($search){
            $where.=" and title like '%".$search."%' ";
        }
        $this->page_title="ئۇچۇر باشقۇرۇش";
        //获取所有的文章
        $count= count(Admin2017Controller::admin_news(
            $id,"dp_cate","dp_article",$where,"dp_article.id","",""
        ));// 查询满足要求的总记录数
        $Page= new \Think\Page($count,$this->pa);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->rollPage=6;
        $Page->lastSuffix=false;
        $this->meige=$Page->listRows;
        $Page->setConfig('next','كىينىكى بەت');
        $Page->setConfig('prev','ئالدىنقى بەت');
        $show= $Page->show();// 分页显示输出
        $articles=Admin2017Controller::admin_news(
            $id,"dp_cate","dp_article",$where
            ,"dp_cate.name,dp_article.id,title,thumb,intime,uptime,author,name,dp_article.status"
            ,$Page->firstRow,$Page->listRows
        );
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("count",$count);
        $this->assign("articles",$articles);
        //所有的粉类
        $classify=_sort(M("cate")->select());
        $this->assign("classify",$classify);
        $this->assign("get_id",$id);
        $this->assign("search",$search);
        $this->display("index");
    }

    //分配Article添加文章页面
    function add(){
        $list=array();
        $classify=_sort(M("cate")->select());
        $this->assign("classify",$classify);
        $this->page_title="ئۇچۇر قوشۇش";
        //获取所有的标签
        $tags=M("tag")->order("id desc")->select();
        //manba
        $list['source']=M("source")->select();
        $this->assign("list",$list);
        $this->assign("tags",$tags);
        $this->display("add");
    }

    //接收数据并且写入到数据库
    public function addhand(){
        //实例化对象
        $user = D("article");
        $post=I("post.");
        if (!$user->create()){//如果创建数据对象失败，就提示
            $this->error($user->getError());
        }else{
            //新增的数据id
            $aid=$user->add();
            //手动添加修改时间
            $uptime=array("uptime"=>time(),"intime"=>strtotime($post['intime']));
            M('article')->where("id='$aid'")->save($uptime);
            if($aid){
                $this->display("../index");
                _check_alert("ئۇچۇر قوشۇش مۇۋاپىقيەتلىك بولدى !","index.html",1);
            }
//            if($post['ok']){
//                IndexController::img();
//            }
            //如果post里面有tag标签,就运行这个函数 addArticle( )
            if(isset($post['tag'])){
                //按照新增的数据id添加标签,id传给 addArticle( )这个函数
                $this->addArticle($post,$aid);
            }
        }
    }

    //编辑分类
    public function edit(){
        $list=array();
        $id=I('get.id');
        $this->page_title="ئۇچۇر تەھرىرلەش";
        //获取要编辑的一条数据
        $articles=M("article")->find($id);
        $this->assign("articles",$articles);
//        p($articles);die;
        //获取所有的分类
        $classify=_sort(M("cate")->select());
        $this->assign('classify',$classify);
        //获取所有的标签
        $tags=M("tag")->order("id desc")->select();
        $this->assign("tags",$tags);
        $tids=M('articletag')->where("aid='$id'")->getField('tid',true);
        //manba
        $list['source']=M("source")->select();
        $this->assign("list",$list);
        $this->assign("tids",$tids);
        $this->display("edit");
    }

    // article里面的标签添加到tag数据表
    private function addArticle($post,$aid){
        if(isset($post['tag']) && is_array($post['tag'])){
            foreach ($post['tag'] as $tag){
                $data=array(
                    'aid'=>$aid,
                    'tid'=>$tag
                );
                M('articletag')->add($data);
            }
        }
    }
    //接收数据并且写入到数据库
    public function savehand(){
        $id=I('post.id');
        //实例化对象
        $user = D("article");
        if (!$user->where("id='$id'")->create()){//如果创建数据对象失败，就提示
            $this->error($user->getError());
        }else{
            $user->where("id='$id'")->save();
            //手动添加修改时间
            $uptime=array("uptime"=>time(),"intime"=>strtotime(I('post.intime')));
            M('article')->where("id='$id'")->save($uptime);
            if($id){
                $this->display("../index");
                _check_alert("ئۇچۇر ئۆزگەرتىش مۇۋاپىقيەتلىك بولدى !","index.html",1);
            }
            $post=I("post.");
            //如果post里面有tag标签,就运行这个函数 addArticle( )
            if(isset($post['tag'])){
                M('articletag')->where("aid='$id'")->delete();
                //按照新增的数据id添加标签,id传给 addArticle( )这个函数
                $this->addArticle($post,$id);
            }
        }
    }

    //上传封面图片处理
    public function upload(){
        $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    './Uploads/',
            'savePath'   =>    'Article/',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        $upload = new \Think\Upload($config);// 实例化上传类
        $info=$upload->upload();
        $thumb=$info['file'];
        $thumbPath='Uploads/'.$thumb['savepath'].$thumb['savename'];//图片的完美地址
        $photo['type']='article_upload';
        $photo['thumb']='/'.$thumbPath;
//        //使用thumb方法生成缩略图
//        $image = new \Think\Image();
//        //打开图像资源
//        $image->open($thumbPath);
//        //裁剪图片
//        $image->thumb(150, 150)->save($thumbPath);
        if($info){
            $row=M("photo")->where("thumb='$thumbPath'")->select();
            if(!$row){
                M('photo')->add($photo);
            }
            $data=array(
                'status'=>1,
                'message'=>'رەسىم يوللاش تامام',
                'src'=>$thumbPath
            );
        }else{
            $data=array(
                'status'=>0,
                'message'=>'رەسىم يوللاش مەغلۇب بولدى !',
            );
        }
        $this->ajaxReturn($data);
    }
//上传封面图片处理
    public function uploads(){
        $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    './Uploads/',
            'savePath'   =>    'photo/',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        $upload = new \Think\Upload($config);// 实例化上传类
        $info=$upload->upload();
        $thumb=$info['file'];
        $thumbPath='Uploads/'.$thumb['savepath'].$thumb['savename'];//图片的完美地址
        $photo['type']='photo';
        $photo['thumb']=$thumbPath;
//        //使用thumb方法生成缩略图
//        $image = new \Think\Image();
//        //打开图像资源
//        $image->open($thumbPath);
//        //裁剪图片
//        $image->thumb(150, 150)->save($thumbPath);
        if($info){
            $row=M("photo")->where("thumb='$thumbPath'")->select();
            if(!$row){
                M('photo')->add($photo);
            }
            $data=array(
                'status'=>1,
                'message'=>"رەسىم يوللاندى",
                'src'=>$thumbPath
            );
        }else{
            $data=array(
                'status'=>0,
                'message'=>'رەسىم يوللاش مەغلۇب بولدى !',
            );
        }
        $this->ajaxReturn($data);
    }
    //用ajax分类状态
    public function sortStatus(){
        $id=I('post.id');
        $post['status']=I('post.status');
        if(!M('article')->where("id='$id'")->save($post)){
            $data=array("status"=>0,"content"=>"مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"content"=>"مۇۋاپقىيەتلىك بولدى !");
        }
        $this->ajaxReturn($data);
    }
    //用ajax删除分类
    public function del(){
        $id=I('post.id');
        //然后删除封面图片来源地址
        $thumb_link=M("article")->where("id='$id'")->getField("thumb",true);
        //图片完美的地址
        $thumb=$thumb_link[0];
        //获取要编辑的一条数据
        $data=M("article")->delete($id);
        if(!$data){
            $data=array("status"=>0,"message"=>"ئۆچۈرۈش مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            //文章删除成功的话就闪烁出封面退片地址
            unlink($thumb);
            //先删除此条数据属于的tag
            M("articletag")->where("aid='$id'")->delete();
            $data=array("status"=>1,"message"=>"ئۆچۈرۈش مۇۋاپىقيەتلىك بولدى !");
        };
        $this->ajaxReturn($data);
    }

    //批量删除分类
    public function batch_del(){
        if(!is_array(I('post.dels'))){
            $this->display("../index");
            _check_alert("تاللانغان مەزمۇن يوق !","index.html",2);
            exit;
        }
        //把数组变成字符串
        $dels=implode(",",I('post.dels'));
        //然后删除封面图片来源地址
        $map['id']  = array('in',$dels);
        //获取多数据的说图片地址
        $thumb_link=M("article")->where($map)->getField("thumb",true);
        //删除多数据
        $list=M("article")->delete($dels);
        if($list!==false) {
            foreach($thumb_link as $thumb){
                //文章删除成功的话就闪烁出封面退片地址
                unlink($thumb);
            }
            //删除属于的tag
            $del['aid']  = array('in',$dels);
            M('articletag')->where($del)->delete();
            $this->display("../index");
            _check_alert("{$list} تال ئۇچۇر ئۆچۈرۈلدى !","index.html",1);
        }else{
            $this->display("../index");
            _check_alert(" ئۆچۈرۈش مەغلۇب بولدى !","index.html",5);
        }
    }
    //用ajax分类状态
    public function page(){
        $id=1;
        $post['page']=I('post.page');
        if(!is_numeric($post['page'])){
            $post['page']=15;
        }
        if(!M('setting_page1')->where("id='$id'")->save($post)){
            $data=array("status"=>0,"content"=>"مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"content"=>"ھەربىر بەتتە ".$post['page']."تال ئۇچۇر كۆرىنىدۇ ");
        }
        $this->ajaxReturn($data);
    }
    //分配Article添加文章页面
    function photo(){
        $photo=M("photo")->select();
        if(!$photo){
            $photo=array();
        }
        $this->assign("photo",$photo);
        $this->display("photo");
    }

   //manba koxux
    public function source(){
        if(IS_AJAX){
            $user=M("source");
            if($user->create()){
                $result = $user->add();
                if($result){
                    $data=array("status"=>1,"message"=>"قوشۇلدى!");
                }else{
                    $data=array("status"=>0,"message"=>"مەغلۇب بولدى!");
                }
                $this->ajaxReturn($data);
            }
        }
        $this->display('source');
    }
//上传封面图片处理
    public function source_uploads(){
        $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    './Uploads/',
            'savePath'   =>    'Article/source/',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        $upload = new \Think\Upload($config);// 实例化上传类
        $info=$upload->upload();
        $thumb=$info['file'];
        $thumbPath='Uploads/'.$thumb['savepath'].$thumb['savename'];//图片的完美地址
//        //使用thumb方法生成缩略图
//        $image = new \Think\Image();
//        //打开图像资源
//        $image->open($thumbPath);
//        //裁剪图片
//        $image->thumb(150, 150)->save($thumbPath);
        if($info){
            $data=array(
                'status'=>1,
                'message'=>"رەسىم يوللاندى",
                'src'=>$thumbPath
            );
        }else{
            $data=array(
                'status'=>0,
                'message'=>'رەسىم يوللاش مەغلۇب بولدى !',
            );
        }
        $this->ajaxReturn($data);
    }
}